package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.crm.domain.pojo.CrmEarlyWarningType;
import com.yu.crm.domain.pojo.CrmEarlyWarningTypeParam;
import com.yu.crm.mapper.CrmEarlyWarningTypeMapper;
import com.yu.crm.service.ICrmEarlyWarningTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 预警类型表 服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-06-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmEarlyWarningTypeService extends ServiceImpl<CrmEarlyWarningTypeMapper, CrmEarlyWarningType> implements ICrmEarlyWarningTypeService {
    @Autowired
    UserCacheManager userCacheManager;
    @Autowired
    CrmEarlyWarningTypeParamService typeParamService;

    @Override
    public void addOrUpdate(CrmEarlyWarningType crmEarlyWarningType) {
        String userId = userCacheManager.getCurrentUser().getUserId();
        if(DataHelpers.isNotEmpty(crmEarlyWarningType.getId())){
            crmEarlyWarningType.setGmtModified(LocalDateTime.now());
            this.updateById(crmEarlyWarningType);
        }else{
            crmEarlyWarningType.setCreateUserId(userId);
            crmEarlyWarningType.setGmtCreate(LocalDateTime.now());
            crmEarlyWarningType.setGmtModified(LocalDateTime.now());
            this.save(crmEarlyWarningType);
        }

    }

    @Override
    public List<CrmEarlyWarningType> findAll() {
//        String userId = userCacheManager.getCurrentUser().getUserId();
//        QueryWrapper<CrmEarlyWarningType> wrapper = new QueryWrapper<>();
//        wrapper.eq("create_user_id",userId);
//        return this.list(wrapper);
        return this.list();
    }

    @Override
    public List<CrmEarlyWarningType> findAllIncludeParam() {
        String userId = userCacheManager.getCurrentUser().getUserId();
        return baseMapper.findAllIncludeParam(userId);
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
        QueryWrapper<CrmEarlyWarningTypeParam>  wrapper = new QueryWrapper<>();
        wrapper.eq("early_warning_type_id",id);
        typeParamService.remove(wrapper);
    }
}
